<?php

/**
 * Description of login
 *
 * @author iLyas
 */
class Login extends CI_Controller{

    function __construct() {
        parent::__construct();
        //check if user is logged in
        //$this->is_logged_in();
    } 
    
    function index(){   
        $this->load->view('user/login');
    }
    
    function validate_credentials(){
        $this->load->model('user_model');
        
        $q = $this->user_model->validate();
        
        if($q){ //if user credentials validated..
            $user_id    = $this->user_model->get_user_id($this->input->post("username"));
            $language   = $this->user_model->get_user_lang($user_id);
            $data = array(
                'username'          => $this->input->post("username"),
                'is_logged_in'      => true,
                'user_id'           => $user_id,
                'user_lang'         => $language
            );
            
            $this->session->set_userdata($data);
            redirect('user/');
        }else{
            $error_message = 'Oh no! Wrong username/password combination.';
            $this->load->view('user/login', $error_message);
        }
    }
    function is_logged_in(){
        $is_logged_in = $this->session->userdata('is_logged_in');
        
        if(isset($is_logged_in) || $is_logged_in == TRUE){
            redirect('/user/profile/members_area', 'refresh');
        }else{
            redirect('/user/login', 'refresh');
            
        }
    }
}

?>
